/*
 * =========================================================================
 *
 *       Filename:  opt3.c
 *
 *
 *        Created:  10/06/2011 03:59:44 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * =========================================================================
 */
#include "find.h"
#include <stdio.h>

static int          list_null(s_list    *list,
                              s_list    *l)
{   int             err = 0;
    if (l->size > 0)
    {
       err = print(l);
       l = NULL;
       list = NULL;
    }
    return (err);
}

int                 name(s_list     **list,
                         char       *elt,
                         int        end)
{
    s_list  *l = NULL;
    s_list  *p = NULL;
    char    *s = NULL;

    l = init();
    p = (*list);
    while (p != NULL)
    {
        if ((s = search_elt(p,elt)) != NULL)
        {
            del(p, elt);
            if (mystrcmp(extract_end(s), elt) == 1)
            {
                l = add(l, s);
            }
        }
        p = p->next;
    }
    clear((*list));
    (*list) = l;
    if(end == 1)
        return (list_null ((*list), l));
    else
        return (0);
}

static int          ret(s_list      **list,
                        s_list      *l,
                        int         end)
{
    clear((*list));
    (*list) = l;
    if (end == 1)
        return (list_null ((*list), l));
    else
        return (0);
}

int                 newer(s_list    **list,
                          char      *file,
                          int       end)
{
    s_list          *l = NULL;
    struct stat     buff;
    struct stat     fst;
    char            *bin = NULL;

    if ((search_elt((*list), file) == NULL) && (open(file, O_RDONLY) == -1))
    {
        bin = mystrcat("./find : `", file);
        bin = mystrcat(bin, "'");
        perror(bin);
        exit (1);
    }
    l = init();
    stat(file, &fst);
    while ((*list) != NULL)
    {
        if (open((*list)->dir, O_RDONLY) != -1)
        {
            stat((*list)->dir, &buff);
            if (buff.st_mtime > fst.st_mtime)
                l = add(l, (*list)->dir);
        }
        else
            my_error("./find: `", (*list)->dir);
        (*list) = (*list)->next;
    }
    return (ret(list, l, end));
   }

int                 atime(s_list    **list,
                          int       time,
                          int       end)
{
    s_list          *l = NULL;
    struct stat     buf;
    struct timeval  tvl;

    l = init();
    gettimeofday(&tvl, NULL);

    while ((*list) != NULL)
    {
        stat((*list)->dir, &buf);
        if (((tvl.tv_sec - buf.st_atime) >= (time * SEC_IN_DAY)) &&
           ((tvl.tv_sec - buf.st_atime) <= ((time + 1) * SEC_IN_DAY)))
            l = add(l, (*list)->dir);
        (*list) = (*list)->next;
    }
    clear(*list);
    (*list) = l;
    if (end == 1)
        return (list_null ((*list), l));
    else
        return (0);
}
